package com.allinpokers.vest.allinvest.service.vest;

import com.allinpokers.vest.allinvest.dao.crazypoker.IosVestInfoDao;
import com.allinpokers.vest.allinvest.dao.crazypoker.IosVestPromoteDao;
import com.allinpokers.vest.allinvest.entity.crazypoker.IosVestInfo;
import com.allinpokers.vest.allinvest.entity.crazypoker.IosVestPromote;
import com.allinpokers.vest.allinvest.entity.crazypoker.example.IosVestInfoExample;
import com.allinpokers.vest.allinvest.entity.crazypoker.example.IosVestPromoteExample;
import com.allinpokers.vest.allinvest.entity.crazypoker.plus.IosVestPromoteChannelTimes;
import com.allinpokers.vest.allinvest.enu.ResponseCodeEnum;
import com.allinpokers.vest.allinvest.enu.VestDataTypeEnum;
import com.allinpokers.vest.allinvest.model.request.PageReq;
import com.allinpokers.vest.allinvest.model.request.vest.IosVestInfoAdCloseReq;
import com.allinpokers.vest.allinvest.model.request.vest.IosVestInfoAddReq;
import com.allinpokers.vest.allinvest.model.request.vest.IosVestInfoDelReq;
import com.allinpokers.vest.allinvest.model.request.vest.IosVestInfoUpdateReq;
import com.allinpokers.vest.allinvest.model.respon.CommonRespon;
import com.allinpokers.vest.allinvest.model.respon.IosVestInfoRespon;
import com.allinpokers.vest.allinvest.model.respon.PageBean;
import com.allinpokers.vest.allinvest.util.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ym
 */
@Service
@Slf4j
public class VestServiceImpl implements IVestService {

    private final String pix = "_";
    @Resource
    private IosVestPromoteDao iosVestPromoteDao;
    @Resource
    private IosVestInfoDao iosVestInfoDao;

    @Override
    public CommonRespon saveSuccessTime(String channelId) {
        log.info("saveSuccessTime channelId={}",channelId);
        if(StringUtils.isEmpty(channelId)) {
            return CommonRespon.failure(ResponseCodeEnum.ERROR);
        }
        IosVestInfoExample example=new IosVestInfoExample();
        example.or().andIosChannelIdEqualTo(channelId);
        List<IosVestInfo> infoList=iosVestInfoDao.selectByExample(example);
        if(infoList.isEmpty()){
            return CommonRespon.failure(ResponseCodeEnum.ERROR);
        }

        IosVestPromote item=new IosVestPromote();
        item.setIosChannelId(channelId);
        item.setType(VestDataTypeEnum.SUCCESS.getId());

        iosVestPromoteDao.insertSelective(item);
        return CommonRespon.success();
    }

    @Override
    public CommonRespon saveDownTime(String channelId) {
        log.info("saveDownTime channelId={}",channelId);
        if(StringUtils.isEmpty(channelId)) {
            return CommonRespon.failure(ResponseCodeEnum.ERROR);
        }
        IosVestInfoExample example=new IosVestInfoExample();
        example.or().andIosChannelIdEqualTo(channelId);
        List<IosVestInfo> infoList=iosVestInfoDao.selectByExample(example);
        if(infoList.isEmpty()){
            return CommonRespon.failure(ResponseCodeEnum.ERROR);
        }

        IosVestPromote item=new IosVestPromote();
        item.setIosChannelId(channelId);
        item.setType(VestDataTypeEnum.DOWN.getId());

        iosVestPromoteDao.insertSelective(item);
        return CommonRespon.success();
    }


    @Override
    public String getOpen(String channelId) {
        IosVestInfoExample example=new IosVestInfoExample();
        example.or().andIosChannelIdEqualTo(channelId);
        List<IosVestInfo> infoList=iosVestInfoDao.selectByExample(example);
        if(infoList.isEmpty()){
            return "";
        }
        //判断如果是关闭状态就返回失败
        IosVestInfo info=infoList.get(0);
        if(info.getAdOpen().equals(0)||StringUtils.isEmpty(info.getTargeUrl())){
            return "";
        }
        return info.getTargeUrl();
    }




    @Override
    public CommonRespon<PageBean<IosVestInfoRespon>> iosVestList(PageReq req){

        List<IosVestPromoteChannelTimes> channelTimesList = iosVestPromoteDao.selectListGroupByChannel();
        log.info("channelTimesList={}", JsonUtils.write(channelTimesList));
        //type_channelid  type 1:下载数量  2:转换数量
        Map<String, Long> channelMap = channelTimesList.stream().collect(Collectors.toMap(e -> e.getType() + pix + e.getIosChannelId(), IosVestPromoteChannelTimes::getTimes));
        PageHelper.startPage(req.getPage(), req.getSize());
        IosVestInfoExample example = new IosVestInfoExample();
        List<IosVestInfo> list = iosVestInfoDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        log.info("channelMap={}", JsonUtils.write(channelMap));
        List<IosVestInfoRespon> beanList = new ArrayList<>();
        list.forEach(e -> {
            Long downTimes = channelMap.getOrDefault(1 + pix + e.getIosChannelId(), 0L);
            Long successTimes = channelMap.getOrDefault(2 + pix + e.getIosChannelId(), 0L);
            IosVestInfoRespon item = IosVestInfoRespon.builder().downTimes(downTimes).successTimes(successTimes).build();
            BeanUtils.copyProperties(e, item);
            beanList.add(item);
        });
        PageBean<IosVestInfoRespon> data = new PageBean<>();
        data.setBeanList(beanList).setLastPage(pageInfo.isIsLastPage()).setTotal(pageInfo.getTotal());
        return CommonRespon.success(data);
    }

    @Override
    public CommonRespon addIosVestInfo(IosVestInfoAddReq req) {
        log.info("addIosVestInfo req={}", JsonUtils.write(req));
        IosVestInfoExample example = new IosVestInfoExample();
        example.or().andIosChannelIdEqualTo(req.getIosChannelId());
        List<IosVestInfo> list = iosVestInfoDao.selectByExample(example);
        if (!list.isEmpty()) {
            return CommonRespon.failure(ResponseCodeEnum.DATA_HAS_EXIST);
        }

        LocalDateTime now = LocalDateTime.now();
        IosVestInfo iosVestInfo = IosVestInfo.builder().createTime(now)
                .updateTime(now).opId(-1).build();
        BeanUtils.copyProperties(req, iosVestInfo);


        iosVestInfoDao.insertSelective(iosVestInfo);
        return CommonRespon.success();
    }

    @Override
    public CommonRespon updateIosVestInfo(IosVestInfoUpdateReq req) {
        log.info("updateIosVestInfo req={}", JsonUtils.write(req));
        LocalDateTime now = LocalDateTime.now();
        IosVestInfo iosVestInfo = IosVestInfo.builder()
                .updateTime(now).opId(-1).build();
        BeanUtils.copyProperties(req, iosVestInfo);
        iosVestInfoDao.updateByPrimaryKeySelective(iosVestInfo);
        return CommonRespon.success();
    }

    @Override
    public CommonRespon closeOp(IosVestInfoAdCloseReq req) {
        log.info("closeOp req={}", JsonUtils.write(req));
        LocalDateTime now = LocalDateTime.now();
        IosVestInfo iosVestInfo = IosVestInfo.builder()
                .updateTime(now).opId(-1).build();
        BeanUtils.copyProperties(req, iosVestInfo);

        iosVestInfoDao.updateByPrimaryKeySelective(iosVestInfo);
        return CommonRespon.success();
    }

    @Override
    public CommonRespon del(IosVestInfoDelReq req) {
        log.info("del req={}",req.getId());
        IosVestInfo iosVestInfo= iosVestInfoDao.selectByPrimaryKey(req.getId());
        if(iosVestInfo!=null){
            iosVestInfoDao.deleteByPrimaryKey(req.getId());
            String iosChannel= iosVestInfo.getIosChannelId();
            IosVestPromoteExample example=new IosVestPromoteExample();
            example.or().andIosChannelIdEqualTo(iosChannel);
            iosVestPromoteDao.deleteByExample(example);
        }
        return CommonRespon.success();
    }










}
